package com.example.jiaju.adapter.HomeAdapter;




import com.bumptech.glide.Glide;
import com.example.jiaju.R;
import com.example.jiaju.entity.DecAndWork;
import com.example.jiaju.entity.Furniture;
import com.example.jiaju.entity.House;
import com.example.jiaju.ui.main.ConcreteFurnitureActivity;
import com.example.jiaju.ui.main.ConcreteHouseActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.TintTypedArray;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class WaterfallAdapter extends RecyclerView.Adapter<WaterfallAdapter.WaterfallViewHolder> {

    private int select;
    private List<House.PageDTO.ListDTO> waterfallLocations;
    private List<DecAndWork.PageDTO> decAndWorkList;
    private List<Furniture.PageDTO.ListDTO> furnitureList=new ArrayList<>();
    private Context context;

    public WaterfallAdapter(int select,List list,Context context) {
        this.select=select;
        switch (select){
            case 0:this.waterfallLocations = list;break;
            case 1:this.decAndWorkList = list;break;
            case 2:this.furnitureList = list;break;
            default:break;
        }
        this.context=context;
    }

    @NonNull
    @Override
    public WaterfallViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.homeview_recycler_item,parent,false);
        WaterfallViewHolder myViewHolder =new WaterfallViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull WaterfallViewHolder holder, @SuppressLint("RecyclerView") int position) {

        Random random=new Random();
        double rating=random.nextDouble()*2+3;//生成[2,5)区间的随机数
        holder.rating.setRating((float) rating);
        switch (select){
            case 0:{
                Glide.with(context).load(waterfallLocations.get(position).houseImg).into(holder.img);
                holder.title.setText(waterfallLocations.get(position).houseName);
                holder.content.setText(waterfallLocations.get(position).houseLabel);
                holder.img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context, ConcreteHouseActivity.class);
                        intent.putExtra("houseListDTO",waterfallLocations.get(position));
                        context.startActivity(intent);
                    }
                });
            }break;
            case 1:{
                DecAndWork.PageDTO dec=decAndWorkList.get(position);
                if(dec.works.size()!=0){
                    DecAndWork.PageDTO.WorksDTO worksDTO=dec.works.get(0);
                    String picList[]=worksDTO.workPic.split(",");
                    Glide.with(context).load(picList[0]).into(holder.img);
                    holder.title.setText(worksDTO.workTitle);
                    holder.content.setText(worksDTO.workDetail);
//                    holder.img.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            Intent intent=new Intent(context, ConcreteFurnitureActivity.class);
//                            intent.putExtra("furniture",furnitureList.get(position));
//                        }
//                    });
                }
            }break;
            case 2:{
                Glide.with(context).load(furnitureList.get(position).furnImg).into(holder.img);
                holder.title.setText(furnitureList.get(position).furnName);
                holder.content.setText(furnitureList.get(position).furnBrand);
                holder.img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context,ConcreteFurnitureActivity.class);
                        intent.putExtra("furnDTO",furnitureList.get(position));
                        context.startActivity(intent);
                    }
                });
            }break;
            default:
                break;
        }

    }

    @Override
    public int getItemCount() {
        switch (select){
            case 0:return waterfallLocations.size();
            case 1:return decAndWorkList.size();
            case 2:return furnitureList.size();
            default:return 0;
        }
    }

    public static class WaterfallViewHolder extends RecyclerView.ViewHolder{


        private ImageView img;
        private TextView title;
        private TextView content;
        private RatingBar rating;
        public WaterfallViewHolder(@NonNull View itemView) {
            super(itemView);
            img=itemView.findViewById(R.id.waterfall_img);
            title=itemView.findViewById(R.id.waterfall_title);
            content=itemView.findViewById(R.id.waterfall_content);
            rating=itemView.findViewById(R.id.waterfall_rating);
        }
    }
}
